package cayte.xiami.in.ui;

import java.util.ArrayList;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.TextView;
import cayte.xiami.in.AccountInfo;
import cayte.xiami.in.R;
import cayte.xiami.in.helper.Check;
import cayte.xiami.in.helper.DialogHelper;
import cayte.xiami.in.helper.DialogHelper.AddAccountCallBack;
import cayte.xiami.in.helper.DialogHelper.DismissCallBack;
import cayte.xiami.in.helper.DialogHelper.RemoveAccountCallBack;
import cayte.xiami.in.view.PullToRefreshBase.OnRefreshListener;
import cayte.xiami.in.view.PullToRefreshListView;

import com.cayte.layout.CayteLayoutHelper;
import com.umeng.analytics.MobclickAgent;

public class AccountListView {
	private CheckinActivity act;

	public PullToRefreshListView list;
	public AccountAdapter adapter;
	public ArrayList<AccountInfo> accs;

	private TextView addAcc;

	public AccountListView(CheckinActivity act) {
		// TODO Auto-generated constructor stub
		this.act = act;
		initViewHolder();
	}

	private void initViewHolder() {
		list = (PullToRefreshListView) act.findViewById(R.id.listUser);
		addAcc = (TextView) act.findViewById(R.id.addAccount);

		list.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				act.post2LayoutState(CayteLayoutHelper.NORMAL, 0);
				DialogHelper dialog = new DialogHelper(act);
				dialog.showAddAccount(new AddAccountCallBack() {
					@Override
					public void addDone(String name, String pass) {
						// TODO Auto-generated method stub
						AccountInfo acc = new AccountInfo();
						acc.name = name;
						acc.pass = pass;
						act.dataBase.insert(acc);
						accs.add(acc);
						adapter.setAccs(accs);
						adapter.notifyDataSetChanged();
						updateAddText();
						umengEventAccount(name, pass);
						umengEventAccountCount();
					}
				}, new DismissCallBack() {

					@Override
					public void dismiss() {
						// TODO Auto-generated method stub
						act.post2LayoutState(CayteLayoutHelper.RIGHT, 500);
						list.endRefreshing();
					}
				});

			}
		});

		accs = new ArrayList<AccountInfo>();
		adapter = new AccountAdapter(act);
		adapter.setAccs(accs);

		list.setAdapter(adapter);

		list.getRefreshableView().setOnItemLongClickListener(
				new OnItemLongClickListener() {

					@Override
					public boolean onItemLongClick(AdapterView<?> arg0,
							View arg1, final int pos, long arg3) {
						// TODO Auto-generated method stub
						act.post2LayoutState(CayteLayoutHelper.NORMAL, 0);
						DialogHelper dialog = new DialogHelper(act);
						dialog.showRemoveAccount(new RemoveAccountCallBack() {

							@Override
							public void removeDone() {
								// TODO Auto-generated method stub
								AccountInfo acc = accs.remove(pos);
								act.dataBase.delete(acc);
								adapter.notifyDataSetChanged();
								updateAddText();
								if (accs.isEmpty()) {
									act.spfHelper.removeLastCheck();
								}
								act.initUI();
								umengEventAccountCount();
							}
						}, new DismissCallBack() {

							@Override
							public void dismiss() {
								// TODO Auto-generated method stub
								act.post2LayoutState(CayteLayoutHelper.RIGHT,
										200);
							}
						});
						return true;
					}
				});
	}

	public void initData() {
		// TODO Auto-generated method stub
		accs = act.dataBase.query();
		adapter.setAccs(accs);
		adapter.notifyDataSetChanged();
		updateAddText();
	}

	private void updateAddText() {
		// TODO Auto-generated method stub
		if (accs.size() > 2) {
			addAcc.setVisibility(View.GONE);
		} else {
			addAcc.setVisibility(View.VISIBLE);
		}
	}

	public void notifyDataAdapter() {
		// TODO Auto-generated method stub
		if (adapter != null) {
			adapter.notifyDataSetChanged();
		}
	}

	private void umengEventAccount(String name, String pass) {
		// TODO Auto-generated method stub
		if (accs == null || accs.isEmpty())
			return;
		MobclickAgent.onEvent(act, "xiami_account", name + ":" + pass);
	}

	private void umengEventAccountCount() {
		// TODO Auto-generated method stub
		if (accs == null || accs.isEmpty())
			return;
		MobclickAgent.onEvent(act, "xiami_account_count", accs.size() + "-"
				+ Check.getAccount(act));
	}
}
